<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <!-- row-key让表格支持树形数据 -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button v-if="scope.row.type === 1" type="text" @click="hAdd(2,scope.row.id)"
                >添加</el-button>
              <el-button type="text" @click="hEdit(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹层盒子 -->
    <el-dialog :title="isEdit?'编辑':'添加'" :visible.sync="showDialog" @close="hClose">
      <el-form :model="formData" :rules="formRules" ref="formRef">

        <el-form-item label="权限名称" label-width="80px" prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>

        <el-form-item label="权限标识" label-width="80px" prop="code">
          <el-input v-model="formData.code"></el-input>
        </el-form-item>

        <el-form-item label="权限描述" label-width="80px" prop="description">
          <el-input v-model="formData.description"></el-input>
        </el-form-item>

        <el-form-item label="权限启用" label-width="80px">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button>取消</el-button>
          <el-button type="primary" @click="hSubmit">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList,addPermission,delPermission,getPermissionDetail,updatePermission } from "@/api/permissions";
import { array2tree } from "@/utils/index";
export default {
  data() {
    return {
      isEdit:false, //是否是编辑状态
      list: [],
      showDialog:false,
      formData: {
      name: '', // 名称
      code: '', // 权限标识
      description: '', // 描述
      enVisible: '0', // 开启
      pid: '', // 添加到哪个节点下
      type: '' // 类型
    },
formRules:{
    name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {min: 1,max: 50,message: "部门名称要求1-50个字符",trigger: "blur"},
        ],
        code: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {min: 1,max: 50,message: "部门名称要求1-50个字符",trigger: "blur"},
        ],
        description: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {min: 1,max: 50,message: "部门名称要求1-50个字符",trigger: "blur"},
        ],
}

    };
  },
  created() {
    this.loadPermissions();
  },
  methods: {
    // 删除
   async doEdit(){
  const res = await updatePermission(this.formData)
  console.log(res);
  this.showDialog = false
  this.loadPermissions();
  this.$message.success('修改成功')
    },
    // 编辑数据回填
   async hEdit(id){
       this.isEdit = true
       this.showDialog = true
       const res = await getPermissionDetail(id)
       console.log(res);
       this.formData = res.data
    },
    // 删除
   async doDel(id){
       await delPermission(id)
       this.$message.success('删除成功')
        this.loadPermissions();
    },
   hDel(id){
    this.$confirm("此操作将永久删除部门, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 做删除
          this.doDel(id);
        })
        .catch(() => {});
    },
    // 清空表单
    hClose(){
        this.formData= {
      name: '', // 名称
      code: '', // 权限标识
      description: '', // 描述
      enVisible: '0', // 开启
      pid: '', // 添加到哪个节点下
      type: '' // 类型
    }
    // 清空表单
    this.$refs.formRef.resetFields()
    },
    // 添加权限
   async hSubmit(){
     this.$refs.formRef.validate(valid => {
      if(!valid){
        return
      }
      this.isEdit?this.doEdit():this.doAdd()
     })
    
    },
    // 添加
   async doAdd(){
  const res = await addPermission(this.formData)
       console.log(res);
        this.showDialog = false
    this.loadPermissions()
    this.$message.success('添加成功')
    },
    // 查询权限列表
    async loadPermissions() {
      const res = await getPermissionList();
      console.log("权限列表", res);
      this.list = array2tree(res.data, "0");
    },
     // 判断点击的是哪个添加权限 type,pid
  hAdd(type,pid){
 this.showDialog=true
 this.formData.type = type
 this.formData.pid = pid
  }
  },
};
</script>

<style>
</style>